package com.atguigu.juc.volatiles;

import java.util.concurrent.TimeUnit;

/**
 * @author yfstart
 * @create 2022-07-07 14:30
 */
public class VolatileNoAtomicDemo {
  public static void main(String[] args) {
    MyNumber myNumber = new MyNumber();
    for (int i = 0; i < 10; i++) {
      new Thread(()->{
        for (int j = 0; j < 1000; j++) {
          myNumber.addPlusPlus();
        }
      },String.valueOf(i)).start();
    }

    try { TimeUnit.MILLISECONDS.sleep(2000); } catch (InterruptedException e) { e.printStackTrace();}

    System.out.println("myNumber.number = " + myNumber.number);
  }
}

class MyNumber {
  // int number;
  volatile int number;

  // public synchronized void addPlusPlus() {
  public void addPlusPlus() {
    number++;
  }
}
